using System;
using System.Collections.Generic;
using CosyTrade.Core;
using NHibernate.Expression;
using Castle.ActiveRecord;

namespace CosyTrade.StoreManagement.Implement
{
    [Addin("TradeSystem")]
    public class TradeSystem : AddinBase, ITradeSystem
    {
        public void CreateExportBill(ExportBill exportBill)
        {
            exportBill.IsNewObject = true;
            if (exportBill.BillItems != null)
            {
                foreach (ExportBillItem item in exportBill.BillItems)
                {
                    item.IsNewObject = true;
                    ActiveRecordMediator<ExportBillItem>.Create(item);
                }
            }
            ActiveRecordMediator<ExportBill>.Create(exportBill);
        }

        public void UpdateExportBill(ExportBill exportBill, Dictionary<Guid, BillState> billStates)
        {
            exportBill.IsModifyObject = !exportBill.IsNewObject;
            if (billStates == null) return;

            if(exportBill.BillItems!=null)
            {
                foreach (ExportBillItem item in exportBill.BillItems)
                {
                    switch (billStates[item.PrimaryKey])
                    {
                        case BillState.IsDelete:
                            item.IsDeleteObject = true;
                            ActiveRecordMediator<ExportBillItem>.Update(item);
                            break;
                        case BillState.IsUpdate:
                            item.IsModifyObject = !item.IsNewObject;
                            ActiveRecordMediator<ExportBillItem>.Update(item);
                            break;
                        case BillState.IsNew:
                            item.IsNewObject = true;
                            ActiveRecordMediator<ExportBillItem>.Update(item);
                            break;
                    }
                }
            }
            ActiveRecordMediator<ExportBill>.Update(exportBill);
        }

        public void DeleteExportBill(Guid exportBillGuid)
        {
            ExportBill exportBill = ActiveRecordMediator<ExportBill>.FindByPrimaryKey(exportBillGuid);
            if (exportBill == null) return;
            
            if(exportBill.IsNewObject) ActiveRecordMediator<ExportBill>.Delete(exportBill);
            else
            {
                exportBill.IsDeleteObject = true;
                ActiveRecordMediator<ExportBill>.Update(exportBill);
            }
        }

        public ExportBill GetExportBillItem(Guid primaryKey)
        {
            return ActiveRecordMediator<ExportBill>.FindOne(Expression.And(Expression.Eq("PrimaryKey", primaryKey), Expression.Eq("IsDeleteObject", false)));
        }

        public IList<ExportBillBase> SearchExportBill(ICriterion criterion, int pageIndex, int pageSize, params Order[] orderBy)
        {
            return ActiveRecordMediator<ExportBillBase>.SlicedFindAll(pageIndex * pageSize, pageSize, orderBy, criterion != null ? Expression.And(criterion, Expression.Eq("IsDeleteObject", false)) : Expression.Eq("IsDeleteObject", false));
        }

        public int CalcExportBillCount(ICriterion criterion)
        {
            return ActiveRecordMediator<ExportBillBase>.Count(criterion != null ? Expression.And(criterion, Expression.Eq("IsDeleteObject", false)) : Expression.Eq("IsDeleteObject", false));
        }

        public ImportBill BuildImportBill(Guid exportBillGuid)
        {
            throw new NotImplementedException();
        }

        public void SaveImportBill(ImportBill importBill)
        {
            throw new NotImplementedException();
        }

        public void DeleteImportBill(Guid importBillGuid)
        {
            throw new NotImplementedException();
        }

        public IList<ImportBillBase> SearchImportBillBase(ICriterion criterion, int pageIndex, int pageSize, params Order[] orderBy)
        {
            throw new NotImplementedException();
        }

        public int CalcImportBillCount(ICriterion criterion)
        {
            throw new NotImplementedException();
        }

        public void CommitImportBill(ImportBill importBill)
        {
            throw new NotImplementedException();
        }
    }
}